package com.woniu.controller;

import com.alibaba.fastjson.JSONArray;
import com.woniu.dto.DynamicCommentDto;
import com.woniu.dto.DynamicDetail;
import com.woniu.dto.DynamicPost;
import com.woniu.pojo.CommunityFavor;
import com.woniu.result.CommonResult;
import com.woniu.service.DynamicService;
import com.woniu.util.QiniuUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/dynamic")
public class DynamicController {

    @Autowired
    private DynamicService dynamicService;

    /**
     * 文件上传
     * @param file 文件
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public CommonResult<String> upload(MultipartFile file){
        System.out.println("*********");
        if(file==null){
            return CommonResult.failed("请选择上传图片");
        }
        try {
            //返回的路径，即可存入数据库，即可以在网上直接访问（new       QiniuUploadUtil().upload）调取Util里面方法；
            //QiniuUploadUtil工具类不需要做任何修改
            //UUID获取文件名(自己编写)
            String fileName = UUID.randomUUID().toString();
            String imgurl = new QiniuUploadUtil().upload(fileName, file.getBytes());
            //返回图片路径
            return CommonResult.success(imgurl);
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResult.failed("上传失败");
        }
    }

    @PostMapping("/save")
    @ResponseBody
    public CommonResult<String> save(@RequestBody String dynamic){
        try {
            //转化成真实的值
            String act = URLDecoder.decode(dynamic,"UTF-8");
            //没有上传图片时参数多了一个 = ，不知道为啥
            act = act.replace("=","");
            //将数据转为map
            Map<String,Object> map = JSONArray.parseObject(act);
            //保存动态到数据库
            dynamicService.save(map);
            return CommonResult.success("发布成功");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return CommonResult.failed("操作失败");
        }
    }

    /**
     * 获取主页动态信息
     * @param type
     * @param page
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public CommonResult list(Integer type,Integer page,Integer userId){
        System.out.println(type+":"+page+":"+userId);
        //获取动态信息,并封装成前端需要的数据类
        List<DynamicPost> dynamicPosts = dynamicService.findByType(type,page,userId);
        return CommonResult.success(dynamicPosts);
    }

    /**
     * 获取指定id的动态详情
     * @param dynamicId
     * @return
     */
    @GetMapping("/dynamicDetail")
    @ResponseBody
    public CommonResult dynamicDetail(Integer dynamicId){
        System.out.println(dynamicId);
        //获取指定动态除评论之外的内容
        DynamicPost dynamicPost = dynamicService.findDynamicById(dynamicId);
        //获取指定动态的评论
        List<DynamicCommentDto> dynamicCommentDtos = dynamicService.findByComment(dynamicId);
        //封装成前端需要的数据
        DynamicDetail dynamicDetail = new DynamicDetail(dynamicPost,dynamicCommentDtos);
        return CommonResult.success(dynamicDetail);
    }

    /**
     * 获取指定动态的点赞人消息
     * @param dynamicId
     * @return
     */
    @GetMapping("/favor")
    @ResponseBody
    public CommonResult favor(Integer dynamicId){
        System.out.println(dynamicId);
        List<CommunityFavor> communityFavors = dynamicService.findFavorById(dynamicId);
        return CommonResult.success(communityFavors);
    }

    /**
     * 对指定动态，保存该用户的点赞
     * @param dynamicId
     * @param userId
     * @return
     */
    @PostMapping("/saveFavor")
    @ResponseBody
    public CommonResult saveFavor(Integer dynamicId,Integer userId){
        System.out.println(dynamicId+":"+userId);
        try {
            dynamicService.saveFavor(dynamicId,userId);
            return CommonResult.success("点赞成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.success("内部错误");
        }

    }
    /**
     * 对指定动态，取消该用户的点赞
     * @param dynamicId
     * @param userId
     * @return
     */
    @DeleteMapping("/deleteFavor")
    @ResponseBody
    public CommonResult deleteFavor(Integer dynamicId,Integer userId){
        System.out.println(dynamicId+":"+userId);
        try {
            dynamicService.deleteFavor(dynamicId,userId);
            return CommonResult.success("取消点赞");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.success("内部错误");
        }

    }

    /**
     * 获取指定动态的评论消息
     * @param dynamicId
     * @return
     */
    @GetMapping("/comment")
    @ResponseBody
    public CommonResult comment(Integer dynamicId){
        System.out.println(dynamicId);
        List<DynamicCommentDto> dynamicCommentDtos = dynamicService.findByComment(dynamicId);
        return CommonResult.success(dynamicCommentDtos);
    }

    @PostMapping("/saveComment")
    @ResponseBody
    public CommonResult saveComment(@RequestBody String my_comment){
        try {
            //转化成真实的值
            String act = URLDecoder.decode(my_comment,"UTF-8");
            act = act.replace("comment=","");
            //将数据转为map
            Map<String,Object> map = JSONArray.parseObject(act);
            //添加评论到数据库
            dynamicService.saveComment(map);
            return CommonResult.success("评论成功");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return CommonResult.failed("评论失败");
        }

    }

}
